package org.example.traing.controller;


import org.example.traing.VO.CategoryVO;
import org.example.traing.entity.Service;
import org.example.traing.service.ServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController

public class ServiceController {


    @Autowired
    ServiceService serviceService;

    @GetMapping("/uid/services/{uid}")
    public ResponseEntity<Object> getService(@PathVariable("uid") String uid) {

        List<Service> serviceList = serviceService.getService(uid);
        Map<String, Object> response = new HashMap<>();
        if (serviceList != null) {

            response.put("msg", "success");
            response.put("data", serviceList);
            return ResponseEntity.ok(response);
        } else {

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }

    @PostMapping("/services")
    public ResponseEntity<Object> addService(@RequestParam String uid,
                                             @RequestParam String type) {

        serviceService.insertService(uid,type);
        Map<String, Object> response = new HashMap<>();
        response.put("msg", "success");
        return ResponseEntity.ok(response);
    }


    @PutMapping("/services")
    public ResponseEntity<Object> updateService(@RequestParam String serviceId,
                                                @RequestParam String status) {


        serviceService.updateService(serviceId,status);
        Map<String, Object> response = new HashMap<>();
        response.put("msg", "success");
        return ResponseEntity.ok(response);
    }

    @DeleteMapping("/services/{id}")
    public ResponseEntity<Object> deleteService(@PathVariable("id") int id) {

        serviceService.deleteService(id);
        Map<String, Object> response = new HashMap<>();
        response.put("msg", "success");
        return ResponseEntity.ok(response);
    }

    @GetMapping("/serviceId/services/{ServiceId}")

    public ResponseEntity<Object> getServiceByServiceId(@PathVariable("ServiceId") int ServiceId) {

        Service service = serviceService.getServiceByServiceId(ServiceId);
        Map<String, Object> response = new HashMap<>();
        if (service != null) {

            response.put("msg", "success");
            response.put("data", service);
            return ResponseEntity.ok(response);
        } else {
            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }

    }
}